package net.siufung.market.base.app.provider;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.market.base.app.mapper.IAppMapper;
import net.siufung.market.base.app.mapper.IAppMenuMapper;
import net.siufung.market.base.app.mapper.entity.App;
import net.siufung.market.base.app.mapper.entity.AppMenu;
import net.siufung.market.base.permit.spi.IPermitAppMenuSpi;
import net.siufung.market.base.permit.spi.dto.PermitAppMenuDto;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 陈建峰
 * @date 2023/8/8 2:16 PM
 * @since V1.0.0
 */
@Primary
@Component
@AllArgsConstructor
public class PermitAppMenuImpl implements IPermitAppMenuSpi {

    private final IAppMenuMapper appMenuMapper;
    private final IAppMapper appMapper;

    @Override
    public List<PermitAppMenuDto> getPermitAppMenuList(String clientId) {
        return null;
    }

    @Override
    public List<Integer> getMenuIdListByClientId(String clientId) {
        App app = appMapper.selectOne(
                new LambdaQueryWrapper<App>().eq(App::getClientId, clientId));
        if(Objects.isNull(app)){
            throw new BizServiceException("无效clientId");
        }
        List<AppMenu> appMenuList = appMenuMapper.selectList(
                new LambdaQueryWrapper<AppMenu>().eq(AppMenu::getAppId, app.getId()));
        return appMenuList.stream().map(AppMenu::getMenuId).collect(Collectors.toList());
    }
}
